package devicedbgui;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/** Responsible for connecting application to database */
public class DBConnection {
    /** Connects to database
     * @param serverName
     * @param username
     * @param password
     * @param mydatabase
     * @return connection
     * @throws Exception 
     */
    public static Connection  getConnection(String serverName, String username, String password, String mydatabase) throws Exception
    {
        Connection connection = null;
        try {             
            // Load the JDBC driver
            String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
            Class.forName(driverName);

            // Create a connection to the database            
            String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url           
            connection = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException ex) {
            throw new Exception(ex.getMessage());
        } catch (SQLException ex) {
            throw new Exception(ex.getMessage());  
        }
        return connection;
    }
}
